/**
 * jims
 */
package com.jims.master.modules.blood.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.blood.dao.BloodInpDetailDao;
import com.jims.master.modules.blood.dao.BloodOutpDetailDao;
import com.jims.master.modules.blood.dao.BloodOutpMasterDao;
import com.jims.master.modules.blood.entity.BloodInpDetail;
import com.jims.master.modules.blood.entity.BloodOutpDetail;
import com.jims.master.modules.blood.entity.BloodOutpMaster;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 血液出库Service
 * @author fengyg
 * @version 2017-04-10
 */
@Service
@Transactional(readOnly = true)
public class BloodExportService extends CrudService<BloodOutpDetailDao, BloodOutpDetail> {
    @Autowired
    private BloodOutpMasterDao bloodOutpMasterDao;
    @Autowired
    private BloodInpDetailDao bloodInpDetailDao;

	public BloodOutpDetail get(String id) {
		return super.get(id);
	}
	
	public List<BloodOutpDetail> findList(BloodOutpDetail bloodOutpDetail) {
		return super.findList(bloodOutpDetail);
	}
	
	public Page<BloodOutpDetail> findPage(Page<BloodOutpDetail> page, BloodOutpDetail bloodOutpDetail) {
		return super.findPage(page, bloodOutpDetail);
	}

    /**
     * 获取当前机构最大入库单号
     * @return
     * @author fengyg
     */
    public String getMaxOutpNo() {
        return bloodOutpMasterDao.getMaxOutpNo(UserUtils.getOrgId());
    }

    /**
     * 血液入库单号自动补全
     * @param q 传递参数
     * @param limit
     * @param bloodOutpDetail
     * @return
     * @author fengyg
     */
    public List<BloodOutpDetail> autoCompleteInpNo(String q,String limit,BloodOutpDetail bloodOutpDetail){
        return dao.autoCompleteInpNo(q,limit,bloodOutpDetail,UserUtils.getUser().getOrgId());
    }

    /**
     * 血液出库保存
     * @param bloodOutpDetail
     * @return
     * @author fengyg
     */
	@Transactional(readOnly = false)
	public int saveBloodExport(BloodOutpDetail bloodOutpDetail) throws Exception{
        int num = 0;
        if (null != bloodOutpDetail) {
            List<BloodOutpDetail> list = bloodOutpDetail.getBloodExportDetailList();
            if (null != list && list.size() > 0) {
                BloodOutpMaster master = list.get(0).getBloodOutpMaster();
                master.preInsert();
                master.updateOrgId();
                master.setOutpDate(new Date());
                int n = bloodOutpMasterDao.insert(master);  //保存血液出库主表
                if (n <= 0) {
                    num = 0;
                    throw new Exception("主表保存失败!");
                } else {
                    num += n;
                }
                for (BloodOutpDetail detail : list) {
                    BloodOutpDetail saveDetail = detail;
                    saveDetail.preInsert();
                    saveDetail.setOutpMasterId(master.getId());
                    saveDetail.updateOrgId();
                    int no = dao.insert(saveDetail);   //保存血液出库明细表
                    if (no <= 0) {
                        num = 0;
                        throw new Exception("血袋号为" + detail.getBloodId() + "的血袋保存失败!");
                    } else {
                        num += no;
                    }
                    //更改对应的入库明细状态
                    BloodInpDetail inpDetail = new BloodInpDetail();
                    inpDetail.setBloodId(detail.getBloodId());
                    inpDetail.setPatBloodGroup(detail.getPatBloodGroup());
                    inpDetail.setRh(detail.getRh());
                    inpDetail.setComponentId(detail.getComponentId());
                    inpDetail.setBloodCapacity(detail.getBloodCapacity());
                    inpDetail.setInvalDate(detail.getInvalDate());
                    inpDetail.setBloodDonorName(detail.getBloodDonorName());
                    inpDetail.setBloodGatherDate(detail.getBloodGatherDate());
                    inpDetail.setUnits(detail.getUnits());
                    inpDetail.setFlag("0");
                    inpDetail.setDelFlag("0");
                    inpDetail.setOrgId(UserUtils.getUser().getOrgId());
                    int i = updateInpDetailFlag(inpDetail);  //更改入库明细状态
                    if (no <= 0) {
                        num = 0;
                        throw new Exception("血袋号为" + detail.getBloodId() + "的血袋入库明细状态更新失败!");
                    } else {
                        num += no;
                    }
                }
            }
        }
        return num;
	}
	
	@Transactional(readOnly = false)
	public int delete(BloodOutpDetail bloodOutpDetail) {
		return super.delete(bloodOutpDetail);
	}

    /**
     * 更改入库明细状态
     * @return
     * @author fengyg
     */
    @Transactional(readOnly = false)
    public int updateInpDetailFlag(BloodInpDetail bloodInpDetail){
        BloodInpDetail inpDetail = bloodInpDetailDao.getInpDetail(bloodInpDetail);
        inpDetail.setFlag("2");
        return bloodInpDetailDao.update(inpDetail);
    }

    /**
     * 出库单查询
     * @param bloodOutpDetail
     * @return
     * @author fengyg
     */
    public List<BloodOutpDetail> bloodExportQuery(BloodOutpDetail bloodOutpDetail){
        bloodOutpDetail.updateOrgId();
        return dao.bloodExportQuery(bloodOutpDetail);
    }
}